//
//  PageViewController.swift
//  PalmCofC
//
//  Created by apple on 2017/6/21.
//  Copyright © 2017年 youjindi. All rights reserved.
//

import UIKit

class PageViewController: UIViewController {
    
    var k:CGFloat = 20
    var controllers:[UIViewController] = []
    var titleA:[String] = []
    var slider = UIView()
    var pageViewController = UIPageViewController()
    var btnW = CGFloat()
    let cv = UIView()
    var btnA:[UIButton] = []
    var isLayerBtn = false
    
    open func setUpData(_ controller: Array<Any>,_ titleA: Array<Any>) -> Void{
        controllers = controller as! [UIViewController]
        self.titleA = titleA as! [String]
        cv.frame = CGRect(x: 0, y: k, width: ScreenWidth, height: 35)
        btnW = cv.frame.width / CGFloat(titleA.count)
        slider.backgroundColor = MainColor
        slider.frame = CGRect(x: btnW/2-k, y: k + btnH, width: 40, height: 3)
        view.addSubview(cv)
        setUpVC()
        setUpButton()
    }
    
    let btnH = CGFloat(35)
    open func setUpVC() -> Void{
        pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        addChildViewController(pageViewController)
        view.addSubview(pageViewController.view)
        pageViewController.didMove(toParentViewController: self)
        pageViewController.view.frame = CGRect(x: 0, y: k + btnH + 3, width: ScreenWidth, height: ScreenHeight - btnH - 3)
        pageViewController.dataSource = self
        pageViewController.setViewControllers([controllers[0]], direction: .forward, animated: true) { (c) in
            
        }
    }
    
    open func setUpButton() -> Void{
        view.addSubview(slider)
        for index in 0..<titleA.count{
            let btn = UIButton(frame: CGRect(x: btnW * CGFloat(index), y: 0, width: btnW, height: btnH))
            btn.setTitle(titleA[index], for: .normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
            btn.setTitleColor(UIColor.black, for: .normal)
            btn.tag = index + 100
            btn.backgroundColor = UIColor.clear
            btn.addTarget(self, action: #selector(PageViewController.changeCurrentPage(_:)), for: .touchUpInside)
            if index == 0 {
                btn.setTitleColor(MainColor, for: .normal)
            }
            btnA.append(btn)
            cv.addSubview(btn)
        }
    }
    
    open func changeCurrentPage(_ sender: UIButton) -> Void{
        currentPage = sender.tag - 100
    }
    
    var lastPage = 0
    var currentPage: Int = 0 {
        didSet{
            if currentPage > lastPage {
                self.pageViewController.setViewControllers([controllers[currentPage]], direction: .forward, animated: true, completion: nil)
            }
            else {
                self.pageViewController.setViewControllers([controllers[currentPage]], direction: .reverse, animated: true, completion: nil)
            }
            lastPage = currentPage
            changeSlider()
            
        }
    }
    
    open func changeSlider() -> Void{
        let offset = view.frame.width / CGFloat(controllers.count) * CGFloat(lastPage)
        UIView.animate(withDuration: 0.3) { () -> Void in
            self.slider.frame.origin.x = offset + self.btnW/2-self.k
        }
        btnA[lastPage].setTitleColor(MainColor, for: .normal)
        for btn in btnA{
            if btn != btnA[lastPage]{
                btn.setTitleColor(UIColor.black, for: .normal)
            }
        }
    }
    
    open func indexOfViewController(_ viewController: UIViewController) -> Int{
        lastPage = controllers.index(of: viewController)!
        changeSlider()
        return lastPage
    }

}

extension PageViewController: UIPageViewControllerDataSource{
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = indexOfViewController(viewController)
        if index == NSNotFound || (index == controllers.count - 1){
            return nil
        }
        index += 1
        return controllers[index]
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        var index = indexOfViewController(viewController)
        if index == 0 || index == NSNotFound{
            return nil
        }
        index -= 1
        return controllers[index]
    }
}
